﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :News_E.aspx.cs
//        description :新闻信息编辑页面
//
//        created by 邹兴武 at  2010-12-29
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace huaguwang.hgmanage.News
{
    public partial class News_E : System.Web.UI.Page
    {
        private readonly D_T_NewsInfo_Exp _dal = new D_T_NewsInfo_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.EditInit(
               UserConst.Exp_GetUser,
               UserConst.Exp_GetButtonList,
               SetBut,
              UserConst.Exp_ToolBarErr
            );
            if (!IsPostBack)
            {
                this.HGDownListTree1.Attributes.Add(
                   "onchange",
                   "javascript:$(\"#" + this.txtName.ClientID + "\").val(\"\")"
                   );

                this.HGDownListTree1.Exp_BindNewsClassTree();

                if (Request.Params["id"] != null)
                {
                    ViewState.Add("id", Request.Params["id"]);
                    InitData(Request.Params["id"].Exp_IntTryParse());
                }
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData(int id)
        {
            var model = _dal.GetModel(id);
            if (model == null)
            {
                var SKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("没有找到可编辑的资料!", "news.aspx?" + SKey);
                return;
            }
            this.HGDownListTree1.Text = model.NewsCateID.Exp_Trim();
            this.txtName.Text = model.Title;
            this.txtSource.Text = model.Source;
            this.FLWUpLoad1.SetIMG(model.Pic);
            this.rblTopis.Text = model.Topis.Exp_Trim();
            this.rblRem.Text = model.RemSign ? "1" : "0";
            this.txtWebTitle.Text = model.WebTitle;
            this.txtWebKey.Text = model.WebKey;
            this.txtWebSummary.Text = model.WebSummary;
            this.txtInnerKey.Text = model.InnerKey;
            this.txtContent.Value = model.NewsContent;

        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static bool Exists(string title, int petid, int id)
        {
            D_T_NewsInfo_Exp _dal = new D_T_NewsInfo_Exp();
            return _dal.Exists(title, petid, id);
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var model = CreateModel();
                if (model == null)
                {
                    return;
                }
                var iRet = 0;
                Log_Type le = new Log_Type();
                if (model.id > 0)
                {
                    var tempModel = _dal.GetModel(model.id);
                    if (tempModel == null || !tempModel.Flag)
                    {
                        this.Exp_PageAlter("当前信息不存在,可能已经被其他人删除!");
                        return;
                    }
                    model.Click = tempModel.Click;
                    if (model.Pic.Length <= 0)
                    {
                        model.Pic = tempModel.Pic;
                    }
                    iRet = _dal.Update(model);
                    le = Log_Type.update;
                }
                else
                {
                    iRet = _dal.Add(model);
                    le = Log_Type.add;
                }

                if (iRet > 0)
                {
                    //写日志
                    this.Exp_PageWriteLog(le, "新闻内容信息", iRet);
                    var SKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("操作成功", "news.aspx?" + SKey);
                }
                else
                {
                    this.Exp_PageAlter("操作失败");
                }
            });
        }

        /// <summary>
        /// 获取model对象
        /// </summary>
        private M_T_NewsInfo CreateModel()
        {
            var model = new M_T_NewsInfo();
            model.id = ViewState["id"].Exp_IntTryParse();
            model.NewsCateID = this.HGDownListTree1.Text.Exp_IntTryParse();
            model.Title = this.txtName.Text.Exp_Trim();
            model.Source = this.txtSource.Text.Exp_Trim();
            model.NewsContent = this.txtContent.Value;
            model.Flag = true;
            model.AddDate = DateTime.Now;
            model.Topis = this.rblTopis.Text.Exp_IntTryParse(); //this.txtTopis.Text.Exp_IntTryParse();
            model.RemSign = this.rblRem.Text == "1";
            model.WebTitle = this.txtWebTitle.Text.Exp_Trim();
            model.WebKey = this.txtWebKey.Text.Exp_Trim();
            model.WebSummary = this.txtWebSummary.Text.Exp_Trim();
            model.ColorTitle = string.Empty;// this.txtColorTitle.Value.Exp_Trim();
            model.ColorKey = string.Empty;// this.txtColorKey.Value.Exp_Trim();
            model.InnerKey = this.txtInnerKey.Text.Exp_Trim();

            if (model.WebTitle.Length <= 0)
            {
                model.WebTitle = model.Title;
            }
            if (model.WebKey.Length <= 0)
            {
                model.WebKey = model.Title;
            }
            if (model.WebSummary.Length <= 0)
            {
                model.WebSummary = model.Title;
            }

            var fileName = ""; 
            if (this.FLWUpLoad1.UpLoad())
            {
                fileName = this.FLWUpLoad1.FilePath;
            }
            else
            {
                fileName = "";
            }
            model.Pic = fileName;

            return model;
        }

        /// <summary>
        /// 返回按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var SKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("news.aspx?" + SKey);
        }

    }
}
